import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';

import '../../../const/sys_const.dart';
import '../../../dao/user_dao.dart';
import '../../../models/const.dart';
import '../../../models/user_login_info_entity.dart';
import '../../../tool/im.dart';
import '../../login/login.dart';

class Settings extends StatefulWidget {
  const Settings({Key? key}) : super(key: key);

  @override
  State<Settings> createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {
  UserLoginInfoEntity _user = UserLoginInfoEntity();
  bool switchValue = true;

  @override
  void initState() {
    super.initState();
    UserDao.info({}).then((value) {
      UserLoginInfoEntity userLoginInfoEntity =
          UserLoginInfoEntity.fromJson(value.data);
      _user = userLoginInfoEntity;
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("设置"),
      ),
      body: ListView(
        children: [
          ListTile(
            title: const Text('头像修改'),
            trailing: GFAvatar(
              size: GFSize.SMALL,
              backgroundImage:
                  NetworkImage(_user.avatar ?? SysConst.defaultAvatar),
            ),
            onTap: () {},
          ),
          const Divider(
            indent: 60,
            height: 0,
          ),
          ListTile(
            title: const Text('手机号修改'),
            trailing: const Text("180******9732"),
            onTap: () {},
          ),
          const Divider(
            indent: 60,
            height: 0,
          ),
          ListTile(
            title: const Text('推送开关'),
            trailing: CupertinoSwitch(
              // This bool value toggles the switch.
              value: switchValue,
              activeColor: CupertinoColors.activeBlue,
              onChanged: (bool? value) {
                // This is called when the user toggles the switch.
                setState(() {
                  switchValue = value ?? false;
                });
              },
            ),
            onTap: () {},
          ),
          const Divider(
            indent: 60,
            height: 0,
          ),
          GFButton(
              onPressed: () {
                //断开IM连接
                IMUtils.disconnect();
                //退出
                UserInfo.logout();
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => const LoginPage()));
              },
              shape: GFButtonShape.pills,
              child: const Text('退出登录')),
          const Divider(
            indent: 60,
            height: 0,
          ),
          GFButton(
            onPressed: () {},
            shape: GFButtonShape.pills,
            type: GFButtonType.outline,
            color: GFColors.DANGER,
            child: const Text('删除账户'),
          ),
          const Divider(
            indent: 60,
            height: 0,
          ),
        ],
      ),
    );
  }
}
